package com.serviceprovider.business.module.user.model.impl;

//~--- non-JDK imports --------------------------------------------------------

import com.serviceprovider.business.module.admin.role.model.Role;
import com.serviceprovider.business.module.admin.role.services.dao.interfaces.IRoleService;
import com.serviceprovider.business.module.admin.user.model.AdminManager;
import com.serviceprovider.business.module.admin.user.services.dao.interfaces.IUserService;
import com.serviceprovider.business.module.sp.payee.model.PayeeManager;
import com.serviceprovider.business.module.sp.user.ServiceProviderManager;
import com.serviceprovider.business.module.user.model.interfaces.IUserManager;
import com.serviceprovider.business.utility.maintenance.settings.RoleUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

/**
 * @Leonardo A. Auer
 * @since 04/11/2011
 */

/**
 * Factory to instantiate classes implementing
 * IUserManager.
 */
@Repository("userManagerFactory")
public class UserManagerFactory {
    @Autowired
    private AdminManager           adminManager;
    @Autowired
    private PayeeManager           payeeManager;
    @Autowired
    private IRoleService           roleService;
    @Autowired
    private ServiceProviderManager spManager;
    @Autowired
    private IUserService           userService;

    /**
     * Instantiate an UserManager according to the
     * user connected's role.
     * @param container
     * @return IUserManager Returns an instance of UserManager
     */
    public IUserManager getManager(UserManagerFactory container) {
        IUserManager userManager = null;

        if (container.getRoleUserConnected().getName().equals(RoleUtils.ROLE_ADMINISTRATOR)) {
            userManager = container.getAdminManager();
        } else if (container.getRoleUserConnected().getName().equals(RoleUtils.ROLE_SERVICE_PROVIDER)) {
            userManager = container.getSpManager();
        } else if (container.getRoleUserConnected().getName().equals(RoleUtils.ROLE_PAYEE)) {
            userManager = container.getPayeeManager();
        }

        return userManager;
    }

    /**
     * Returns the role of the user connected.
     * @return Role
     */
    private Role getRoleUserConnected() {
        User userConnected = userService.getUserConnected();

        roleService.retrieveRole(userConnected);

        return userConnected.getRole();
    }

    public AdminManager getAdminManager() {
        return adminManager;
    }

    public ServiceProviderManager getSpManager() {
        return spManager;
    }

    public PayeeManager getPayeeManager() {
        return payeeManager;
    }
}



